<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closure">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          style="width:50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="departmentNameFocus" />
        <div v-if="showTree" class="tree-box">
          <el-tree :data="depts" :props="defaultProps" @node-click="selectNode" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closure">取消</el-button>
      <el-button type="primary" @click="addEmp">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// const dayjs = require('dayjs')
// 导入局部组件必须在scrip里面导入,在App里面导入
import { addEmployees } from '@/api/employees'

export default {
// 组件名称
  name: '',
  // 接收局部注册的组件
  components: {},
  // 组件参数接收来自父组件的数据
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    depts: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  // 组件数据
  data () {
    return {
      showTree: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      options: [
        {
          value: '1',
          label: '正式员工'
        }, {
          value: '2',
          label: '临时员工'
        }

      ],
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 校验
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 4, message: '请输入2-4个字', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // g关闭弹窗
    closure () {
      this.$emit('closure')
      // 清空form里面的值
      for (const key in this.formData) {
        this.formData[key] = ''
      }
      this.$refs.formData.resetFields()
    },
    // 新增
    addEmp () {
      this.$refs.formData.validate(async valid => {
        if (valid) {
          await addEmployees(this.formData)
          this.$emit('updates')
          this.closure()
        }
      })
    },
    // 获得焦点
    departmentNameFocus () {
      console.log(1)
      this.showTree = true
    },
    // 点击选择部门
    selectNode (data) {
      // 该还有子节点, 就是展开操作, 不选中
      if (data.children && data.children.length > 0) return
      this.formData.departmentName = data.name
      this.showTree = false
      console.log(data.name)
    }
  }

}
</script>
<style lang='less' scoped>

</style>
